# copyright Satsang Technologies Limited 2002-2007
require "digest/sha1"

class User < ActiveRecord::Base
  attr_accessor :txtpassword
  attr_accessible :username, :txtpassword,:company_id,:email, :profile_id
  
  validates_presence_of :username,:txtpassword,:company_id,:email, :profile_id
  validates_uniqueness_of :username
  
  belongs_to :company
  has_many :project
  has_many :architecture
  has_one :profile
  
  def before_create
      self.password = User.hash_password(self.txtpassword)
  end
  
  def after_create
    @password = nil
  end
  
  def self.hash_password(txtpassword)
    Digest::SHA1.hexdigest(txtpassword)
  end
  
  def self.login(name, password)
    hashed_password = hash_password(password || "")
    find(:first, :conditions => ["username = ? and password = ?", name, hashed_password])
  end

  def try_to_login
    User.login(self.username, self.txtpassword)
  end
  def isadmin
    company = Company.find(id)
    if company
        isadmin = company.account_type
    end
  end
end
